package com.customview.wzq.zrwidgetdemo;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * Implementation of App Widget functionality.
 */
public class ZrWidget extends AppWidgetProvider {

    private  CharSequence test;

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
      /*  Log.d("kb_jay","-------------------");
        if(intent!=null){
            test=String.valueOf(intent.getIntExtra("test", 0));
        }
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        ComponentName provider=new ComponentName(context, ZrWidget.class);
        //获得，所有利用AppWidgetProvider作为广播接收器的那些widget的id
        int[] ids=awm.getAppWidgetIds(provider);
        updateAppWidget(context,awm,ids[0]);*/

    }

     void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
        Intent intent = new Intent(context, ListWidgeService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.zr_widget);
        SharedPreferences sp = context.getSharedPreferences(
                String.valueOf(appWidgetId), context.MODE_PRIVATE);
        String mode = sp.getString(ListWidgeService.KEY_MODE,
                ListWidgeService.MODE_IMAGE);
      //  rv.setTextViewText(R.id.tv_list_widget_title,test);
        if (ListWidgeService.MODE_MEDIA.equals(mode)) {
            rv.setTextViewText(R.id.tv_list_widget_title, "视频列表");
        } else {
            Log.d("kb_jay","++++++++++++++++++---------");
            rv.setTextViewText(R.id.tv_list_widget_title, "图像列表");
        }

        rv.setRemoteAdapter(appWidgetId,R.id.list,intent);
        rv.setEmptyView(R.id.list, R.id.list_empty);

        Intent viewIntent = new Intent(Intent.ACTION_VIEW);
        PendingIntent pi = PendingIntent.getActivity(context, 0,
                viewIntent, 0);
        rv.setPendingIntentTemplate(R.id.list, pi);
        appWidgetManager.updateAppWidget(appWidgetId, rv);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }


    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        for(int i=0;i<appWidgetIds.length;i++){
            SharedPreferences sp=context.getSharedPreferences(String.valueOf(appWidgetIds[i]), context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sp.edit();
            editor.clear();
            editor.commit();
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
        context.startService(new Intent(context, MediaService.class));
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
        context.stopService(new Intent(context, MediaService.class));
    }
}

